package ipfms.stripes.maintenance;

/**
 * @author haqqiem
 * 17 Jan 2012 5:25:03 PM
 */
import ipfms.dao.*;
import java.util.List;
import java.util.Date;
import ipfms.model.*;
import able.stripes.JSP;
import java.util.ArrayList;
import com.google.inject.Inject;
import org.apache.log4j.Logger;
import ipfms.stripes.util.SendMail;
import ipfms.stripes.BaseActionBean;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;

@UrlBinding("/report_form")
public class ReportFormActionBean extends BaseActionBean {
    private static final Logger logger = Logger.getLogger(ReportFormActionBean.class);

    private Pengguna pengguna;
    private FaultyReport fReport = new FaultyReport();
    private Building building = new Building();
    private String build_id;
    private boolean flag = false;
    private static boolean flagCode = false;
    private String report;
    private String feedback;
    private static String idReport;
    private String code_class = null;
    private String selectedTab = "0";
    private String code_maintenance = null;
    private List<Building> listOfBuilding = new ArrayList<Building>();

    @Inject
    private BuildingDAO buildingDAO;

    @Inject
    private CodeMaintenanceDAO codeMaintenanceDAO;

    @Inject
    private FaultyReportDAO fReportDAO;

    @Inject
    private CodeClassificationDAO codeClassificationDAO;

    @Inject
    private SendMail sm;

    @DefaultHandler
    public Resolution showForm() {
        pengguna = getContext().getCurrentUser();
        listOfBuilding = buildingDAO.findAll();
        flagCode = false;
        idReport = null;
//        return new JSP("maintenance/tab_maintenence.jsp");
        return new JSP("maintenance/report_form.jsp");
    }

    public Resolution searching() {
        logger.info("build_id : " + build_id);
        building = buildingDAO.findById(Integer.parseInt(build_id));
        if (building != null) {
            flag = true;
            listOfBuilding = buildingDAO.findAll();
            logger.info("building.getIdBuilding() : " + building.getIdBuilding());
        } else {
        }

        return new JSP("maintenance/report_form.jsp");
//        return new JSP("maintenance/tab_maintenence.jsp");
    }

    public Resolution details() {
        String idBuilding = getContext().getRequest().getParameter("idBuilding");
        String idRep = getContext().getRequest().getParameter("idReport");
        build_id = idBuilding;
        building = buildingDAO.findById(Integer.parseInt(build_id));
        if (building != null) {
            flag = true;
            listOfBuilding = buildingDAO.findAll();
            fReport = fReportDAO.findById(Integer.parseInt(idRep));
            idReport = idRep;
            report = fReport.getRemarks();
            code_class = fReport.getCode().getCode();
            flagCode = true;

            logger.info("building.getIdBuilding() : " + building.getIdBuilding());
        }

        return new JSP("maintenance/report_form.jsp");
    }

    public Resolution save() {
        building = buildingDAO.findById(Integer.parseInt(build_id));
        
        pengguna = getContext().getCurrentUser();

        try{
            if(flagCode){
                fReport = fReportDAO.findById(Integer.parseInt(idReport));
                if(fReport != null){
                    InfoAudit ia = new InfoAudit();
                        ia.setDimasukOleh(fReport.getInfoAudit().getDimasukOleh());
                        ia.setTarikhMasuk(fReport.getInfoAudit().getTarikhMasuk());
                        ia.setDiKemaskiniOleh(pengguna);
                        ia.setTarikhKemaskini(new Date());

                    CodeMaintenance cm = codeMaintenanceDAO.findById(code_maintenance);
                    fReport.setMaintenance(cm);

                    fReportDAO.saveOrUpdate(fReport);
                    fReport.setInfoAudit(ia);
                    fReport.setAction("Submitted to Management Commitee.");
                    fReport.setRemarks(report);

                    addSimpleMessage("Your complaint has been submitted. Action will be taken immediately");
                    String contens = "We have received your complaint. Action will be taken immediately.\n" +
                                            "\nID Report : "+fReport.getIdReport().toString()+"\n"+"Report : "+fReport.getCode().getName()+
                                            "\nType of Maintenance : "+fReport.getMaintenance().getName()+"\n";

                    sm.mail(contens);
                }
            }else{
                CodeClassification cc = codeClassificationDAO.findById(code_class);

                InfoAudit ia = new InfoAudit();
                    ia.setDimasukOleh(pengguna);
                    ia.setTarikhMasuk(new Date());

                if(building !=null){
                    fReport.setBuilding(building);
                    fReport.setRemarks(report);
                    fReport.setStatus('A');
                    fReport.setInfoAudit(ia);
                    fReport.setCode(cc);
                    fReport.setAction("Submitted to Call Center");

                    fReportDAO.saveOrUpdate(fReport);
                    logger.info(fReport.getIdReport());

                    addSimpleMessage("Your complaint has been submitted. Your complaint id is "+fReport.getIdReport());
                    showForm();
                }
            }
        }catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            addSimpleError("Unsuccessfull");
        }
        return showForm();
    }

    public Pengguna getPengguna() {
        return pengguna;
    }

    public void setPengguna(Pengguna pengguna) {
        this.pengguna = pengguna;
    }

    public FaultyReport getfReport() {
        return fReport;
    }

    public void setfReport(FaultyReport fReport) {
        this.fReport = fReport;
    }

    public Building getBuilding() {
        return building;
    }

    public void setBuilding(Building building) {
        this.building = building;
    }

    public List<Building> getListOfBuilding() {
        return listOfBuilding;
    }

    public void setListOfBuilding(List<Building> listOfBuilding) {
        this.listOfBuilding = listOfBuilding;
    }

    public String getBuild_id() {
        return build_id;
    }

    public void setBuild_id(String build_id) {
        this.build_id = build_id;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getReport() {
        return report;
    }

    public void setReport(String report) {
        this.report = report;
    }

    public String getCode_class() {
        return code_class;
    }

    public void setCode_class(String code_class) {
        this.code_class = code_class;
    }

    public String getSelectedTab() {
        return selectedTab;
    }

    public void setSelectedTab(String selectedTab) {
        this.selectedTab = selectedTab;
    }

    public String getCode_maintenance() {
        return code_maintenance;
    }

    public void setCode_maintenance(String code_maintenance) {
        this.code_maintenance = code_maintenance;
    }

    public boolean isFlagCode() {
        return flagCode;
    }

    public void setFlagCode(boolean flagCode) {
        this.flagCode = flagCode;
    }

    public String getIdReport() {
        return idReport;
    }

    public void setIdReport(String idReport) {
        this.idReport = idReport;
    }

    public String getFeedback() {
        return feedback;
    }

    public void setFeedback(String feedback) {
        this.feedback = feedback;
    }
}
